namespace SimpleWebServices.Web.UI.Html.Elements
{
    using System;
    using System.Xml;
    using System.Xml.Serialization;

    [XmlRoot("table")]
    public class Table : InnerElements, IXmlSerializable
    {
        public string Border
        {
            get;
            set;
        }

        public string CellPadding
        {
            get;
            set;
        }

        public string CellSpacing
        {
            get;
            set;
        }

        public string Frame
        {
            get;
            set;
        }

        public string Rules
        {
            get;
            set;
        }

        public string Summary
        {
            get;
            set;
        }

        public string Width
        {
            get;
            set;
        }

        public override IElement Deserialize(string name, string type, string xml)
        {
            IElement result = null;

            if (null == type && "table".Equals(name, StringComparison.OrdinalIgnoreCase))
            {
                result = xml.Deserialize<Table>();
            }

            return result;
        }

        public override void ReadXml(XmlReader reader)
        {
            string value = reader.GetAttribute("border");
            if (!string.IsNullOrEmpty(value))
            {
                this.Border = value;
            }

            value = reader.GetAttribute("cellpadding");
            if (!string.IsNullOrEmpty(value))
            {
                this.CellPadding = value;
            }

            value = reader.GetAttribute("cellspacing");
            if (!string.IsNullOrEmpty(value))
            {
                this.CellSpacing = value;
            }

            value = reader.GetAttribute("frame");
            if (!string.IsNullOrEmpty(value))
            {
                this.Frame = value;
            }

            value = reader.GetAttribute("rules");
            if (!string.IsNullOrEmpty(value))
            {
                this.Rules = value;
            }

            value = reader.GetAttribute("summary");
            if (!string.IsNullOrEmpty(value))
            {
                this.Summary = value;
            }

            value = reader.GetAttribute("width");
            if (!string.IsNullOrEmpty(value))
            {
                this.Width = value;
            }

            base.ReadXml(reader);
        }

        public override void WriteXml(XmlWriter writer)
        {
            if (!string.IsNullOrEmpty(this.Border))
            {
                writer.WriteAttributeString("border", this.Border);
            }

            if (!string.IsNullOrEmpty(this.CellPadding))
            {
                writer.WriteAttributeString("cellpadding", this.CellPadding);
            }

            if (!string.IsNullOrEmpty(this.CellSpacing))
            {
                writer.WriteAttributeString("cellspacing", this.CellSpacing);
            }

            if (!string.IsNullOrEmpty(this.Frame))
            {
                writer.WriteAttributeString("frame", this.Frame);
            }

            if (!string.IsNullOrEmpty(this.Summary))
            {
                writer.WriteAttributeString("rules", this.Rules);
            }

            if (!string.IsNullOrEmpty(this.Summary))
            {
                writer.WriteAttributeString("summary", this.Summary);
            }

            if (!string.IsNullOrEmpty(this.Width))
            {
                writer.WriteAttributeString("width", this.Width);
            }

            base.WriteXml(writer);
        }
    }
}